package cn.edu.scau.cmi.xuchuangxiong.client;

import java.util.ArrayList;
import java.util.Scanner;

import cn.edu.scau.cmi.xuchuangxiong.composite.safe.Organization;
import cn.edu.scau.cmi.xuchuangxiong.composite.safe.Person;
import cn.edu.scau.cmi.xuchuangxiong.composite.safe.Team;

public class SafeCompositeClient {
	public static void main(String[] args) {
		Scanner sci = new Scanner(System.in);
		Scanner scs = new Scanner(System.in);
		ArrayList<Team> teams = new ArrayList<Team>();
		String name = null;
		while(true)
		{

			System.out.print("1. 创建个人   2. 创建团队   3. 加入团队   4. 显示团队   5. 删除团队\n请输入操作：");
			int choose = sci.nextInt();
			if(choose == 1)
			{
				System.out.print("请输入个人名字：");
				name = scs.nextLine();
				Team person = new Person(name);
				teams.add(person);
			}
			if(choose == 2)
			{
				System.out.print("请输入团队名称：");
				name = scs.nextLine();
				Organization organization = new Organization(name);
				System.out.print("请输入团队成员：");
				name = scs.nextLine();
				String[] members = name.split(" ");
				for(int i = 0; i < members.length; i++)
				{
					int flag = 0;
					for(Team t : teams)
					{
						if(t.getName().equals(members[i]))
						{
							organization.addMember(t);
							teams.remove(t);
							flag = 1;
							break;
						}
					}
					if(flag == 0)
					{
						Team t = new Person(members[i]);
						organization.addMember(t);
					}
				}
				teams.add(organization);
 			}
			if(choose == 3)
			{
				System.out.print("请输入要加入的团队：");
				name = scs.nextLine();
				Organization neworg = null;
				int index = 0;
				for(Team t : teams)
				{
					if(t.getName().equals(name))
					{
						neworg = (Organization)t;
						index = teams.indexOf(t);
						teams.remove(t);
						break;
					}
				}
				System.out.print("请输入要加入的成员：");
				name = scs.nextLine();
				String[] members = name.split(" ");
				for(int i = 0; i < members.length; i++)
				{
					int flag = 0;
					for(Team t : teams)
					{
						if(t.getName().equals(members[i]))
						{
							neworg.addMember(t);
							teams.remove(t);
							flag = 1;
							break;
						}
					}
					if(flag == 0)
					{
						Team t = new Person(members[i]);
						neworg.addMember(t);
					}
				}
				teams.add(index, neworg);
			}
			if(choose == 4)
			{
				int i = 1;
				for(Team t : teams)
				{
					System.out.println("第" + i + "个团队：");
					t.setLevel(0);
					t.printMember();
					i++;
				}
			}
			if(choose == 5)
			{
				System.out.print("请输入需要删除的团队名称：");
				name = scs.nextLine();
				for(Team t : teams)
				{
					if(t.getName().equals(name))
					{
						teams.remove(t);
						break;
					}
				}
			}
		}
		
	}
}
